package com.frejoys.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.frejoys.common.dto.order.OrderStorePageDto;
import com.frejoys.common.vo.order.StoreOrderPageVo;
import com.frejoys.dao.entity.Order;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.List;

public interface OrderMapper extends BaseMapper<Order> {

    // 转积分
    @Update("update j_order set is_score=#{newScoreStatus} where id=#{id} and is_score=#{oldScoreStatus} and verify=#{verify} and pay_status=#{payStatus}")
    boolean upScoreStatusAndRebateMoney(@Param("id") Integer id, @Param("newScoreStatus") Integer newScoreStatus, @Param("oldScoreStatus") Integer oldScoreStatus, @Param("payStatus") Integer payStatus, @Param("verify") Integer verify);

    // 获取订单信息
    @Select("select * from j_order where id=#{id} for update")
    Order getForUpdateInfo(@Param("id") Integer id);

    // 更新核销订单状态
    @Update("update j_order set verify=#{newVerify} where id=#{id} and verify=#{oldVerify}")
    boolean upVerifyInfo(@Param("id") Integer id, @Param("oldVerify") Integer oldVerify, @Param("newVerify") Integer newVerify);

    // 更新核销订单状态
    @Update("update j_order set verify=#{newVerify} where order_sn=#{orderSn} and verify=#{oldVerify}")
    boolean upVerifyInfoByOrderSn(@Param("orderSn") Long orderSn, @Param("oldVerify") Integer oldVerify, @Param("newVerify") Integer newVerify);

    // 获取订单码
    @Select("select qr_code from j_order where order_sn=#{orderSn} limit 1")
    String getQrCodeByOrderSn(@Param("orderSn") Long orderSn);

    // 更新订单码
    @Update("update j_order set qr_code=#{qrCode} where order_sn=#{orderSn}")
    Boolean upQrCode(@Param("orderSn") Long orderSn, @Param("qrCode") String qrCode);

    // 更新返利金额
    @Update("update j_order set verify=#{newVerify} where order_sn=#{orderSn} and pay_status=#{payStatus} and verify=#{oldVerify} and is_score=#{oldScoreStatus}")
    Boolean upRebateMoney(@Param("orderSn") Long orderSn, @Param("newVerify") Integer newVerify, @Param("payStatus") Integer payStatus, @Param("oldVerify") Integer oldVerify, @Param("oldScoreStatus") Integer oldScoreStatus);

    // 获取订单核销信息
    @Select("select user_id,store_id,store_activity_id,price,verify,is_score from j_order where order_sn=#{orderSn} and pay_status=#{payStatus} and verify=#{verify} and is_score=#{isScore} and status=#{status} limit 1")
    Order getOrderVerifyInfo(@Param("orderSn") Long orderSn, @Param("payStatus") Integer payStatus, @Param("verify") Integer verify, @Param("isScore") Integer isScore, @Param("status") Integer status);

    /**
     * 商家查询订单数据
     */
    List<StoreOrderPageVo> pageOrderList(Page page, @Param("dto") OrderStorePageDto dto, @Param("storeIds") List<Integer> storeIds);


    // 获取订单店铺id
    @Select("select store_id from j_order where order_sn=#{orderSn} limit 1")
    Order getStoreIdByOrderSn(@Param("orderSn") Long orderSn);

    //查询已完成订单数量
    @Select("select count(*) from j_order where store_id = #{storeId} and verify >= #{verify} and status = #{status}")
    Integer orderCount(@Param("storeId") Integer storeId,@Param("verify") Integer verify,@Param("status") Integer status);

    //查询店铺订单状态
    @Select("SELECT id,verify,is_score,pay_status FROM `j_order` where store_id = #{storeId} and create_time >= #{startTime} and create_time <= #{endTime} and `status` = #{status}")
    List<Order> getOrderVerifyAndIsScore(@Param("storeId") Integer storeId,@Param("startTime") Integer startTime,@Param("endTime") Integer endTime,@Param("status") Integer status);
}
